#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<math.h>

int IsPrime(int num)
{
    if (num <= 1)
    {
        return 0;
    }

    int i;
    for (i = 2; i <= sqrt(num); i++)
    {
        if (num % i == 0)
        {
            return 0;
        }
    }

    return 1;
}

int main()
{
    int m, n, i, j;
    scanf("%d %d", &m, &n);

    int arr[50][50];

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }

    int Is_Loney_Prime = 1;
    int Loney_Prime = 0;

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            if (IsPrime(arr[i][j]))
            {
                //check neighbors:
                Is_Loney_Prime = 1;

                int dx, dy;
                for (dx = -1; dx <= 1; dx++)
                {
                    for (dy = -1; dy <= 1; dy++)
                    {
                        if (dx == 0 && dy == 0)
                        {
                            continue;
                        }
                        int x = i + dx;
                        int y = j + dy;
                        if (x >= 0 && x < m && y >= 0 && y < n
                            && IsPrime(arr[x][y]))
                        {
                            Is_Loney_Prime = 0;
                            break;
                        }
                    }
                    if (!Is_Loney_Prime)
                    {
                        break;
                    }
                }
                if (Is_Loney_Prime)
                {
                    Loney_Prime++;
                }
            }
        }
    }

    printf("%d", Loney_Prime);

    return 0;
}